package ericXie.com.view 
{
	import com.greensock.TweenLite;
	import ericXie.com.command.SimpleButtonAshyCommand;
	import ericXie.com.component.HaloMovieClip;
	import ericXie.com.component.RouletteMovieClip;
	import ericXie.com.events.ReadDataEvent;
	import ericXie.com.events.SimpleButtonAshyEvent;
	import ericXie.com.model.MainModel;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...转盘显示界面
	 * @author EricXie
	 */
	public class RouletteView extends Sprite 
	{
		//转盘背景
		private var _rouletteBg:MovieClip;
		//转盘
		private var _roulette:RouletteMovieClip;
		//箭头
		private var _arrows:MovieClip;
		//中心变化旋转区
		private var _halo:HaloMovieClip;
		//开始旋转按钮
		private var _startButton:SimpleButton;
		private var _model:MainModel=MainModel.getInstance();
		
		public function RouletteView() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addStageHandler)
		}
		
		private function addStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addStageHandler);
			addRouletteBg();
			addRoulette();
			addArrows();
			addHalo();
			addStartButton();
		}
		
		//添加转盘背景
		private function addRouletteBg():void 
		{
			_rouletteBg = _model.extractionElements("RouletteBackground") as MovieClip;
			this.addChild(_rouletteBg);
			setCoordinate(_rouletteBg);
		}
		//添加箭头
		private function addArrows():void 
		{
			_arrows = _model.extractionElements("Arrows") as MovieClip;
			this.addChild(_arrows);
			setCoordinate(_arrows);
		}
		//添加转盘，并且设置转盘自动旋转为假，存储到存储器
		private function addRoulette():void 
		{
			_roulette = new RouletteMovieClip();
			this.addChild(_roulette);
			setCoordinate(_roulette);
			_roulette.autoRotation = true;
			_model.roulette = _roulette;
		}
		//添加开始旋转按钮，存储到存储器
		private function addStartButton():void 
		{
			_startButton = _model.extractionElements("StartButton") as SimpleButton;
			this.addChild(_startButton);
			setCoordinate(_startButton);
			_model.startBn = _startButton;
			_startButton.addEventListener(MouseEvent.CLICK,startBnClickHandler)
		}
		//添加中心旋转区域，存储到存储器
		private function addHalo():void 
		{
			_halo = new HaloMovieClip();
			this.addChild(_halo);
			setCoordinate(_halo);
			_model.halo = _halo;
		}
		//设置显示对象居中放置
		private function setCoordinate(disp:DisplayObject):void
		{
			disp.x = (stage.stageWidth) / 2;
			disp.y = (stage.stageHeight) / 2;
		}
		//当点击开始旋转按钮时，设置按钮置灰状态，读取数据，让转盘旋转起来，等待数据的进入，旋转速度为10
		private function startBnClickHandler(e:MouseEvent):void 
		{
			_roulette.rotateSpeed = 10;
			new SimpleButtonAshyEvent(_startButton, false).dispatch();
			new ReadDataEvent(MainModel.START_ROULETTE_PATH+new Date().getTime()).dispatch();
		}
		
	}

}